<?php
namespace app\controller;

use app\BaseController;
use app\model\Dossier;
use app\model\LendLog;
use app\model\Thumb;
use think\facade\Request;

class Index extends BaseController
{
    public function index()
    {
        return <<<str
        <!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"><link rel=stylesheet href=//at.alicdn.com/t/font_830376_qzecyukz0s.css><title>vue-manage-system</title><link href=static/css/403.d01b525d.css rel=prefetch><link href=static/css/404.e3be0948.css rel=prefetch><link href=static/css/chart.ca323660.css rel=prefetch><link href=static/css/dashboard.edf7e19b.css rel=prefetch><link href=static/css/drag.bbb2ef3f.css rel=prefetch><link href=static/css/editor.46355876.css rel=prefetch><link href=static/css/home.9ee25996.css rel=prefetch><link href=static/css/i18n.9ed68024.css rel=prefetch><link href=static/css/icon.92119af8.css rel=prefetch><link href=static/css/login.01956d4b.css rel=prefetch><link href=static/css/markdown.6c3bccd0.css rel=prefetch><link href=static/css/permission.f6fb9b47.css rel=prefetch><link href=static/css/table.89c715b9.css rel=prefetch><link href=static/css/tabs.aa11ab42.css rel=prefetch><link href=static/css/upload.9b2acc8e.css rel=prefetch><link href=static/js/403.bd4a0936.js rel=prefetch><link href=static/js/404.543dd137.js rel=prefetch><link href=static/js/chart.b4c00723.js rel=prefetch><link href=static/js/chunk-d3cfa50c.f000faea.js rel=prefetch><link href=static/js/dashboard.b1f33a38.js rel=prefetch><link href=static/js/donate.005af4f5.js rel=prefetch><link href=static/js/drag.28baff91.js rel=prefetch><link href=static/js/dragdialog.3ff27cd8.js rel=prefetch><link href=static/js/editor.1768767b.js rel=prefetch><link href=static/js/form.77c6a83a.js rel=prefetch><link href=static/js/home.f0b22501.js rel=prefetch><link href=static/js/i18n.1e2e42e5.js rel=prefetch><link href=static/js/icon.ff046a7a.js rel=prefetch><link href=static/js/login.ca2f9f08.js rel=prefetch><link href=static/js/markdown.5f21b4e3.js rel=prefetch><link href=static/js/permission.7255618a.js rel=prefetch><link href=static/js/table.27aa55d0.js rel=prefetch><link href=static/js/tabs.44eae09b.js rel=prefetch><link href=static/js/upload.7f1a6687.js rel=prefetch><link href=static/css/app.2b293855.css rel=preload as=style><link href=static/css/chunk-vendors.79db45c7.css rel=preload as=style><link href=static/js/app.a9157c72.js rel=preload as=script><link href=static/js/chunk-vendors.1a04c04c.js rel=preload as=script><link href=static/css/chunk-vendors.79db45c7.css rel=stylesheet><link href=static/css/app.2b293855.css rel=stylesheet></head><body><noscript><strong>We're sorry but vms doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id=app></div><script src=static/js/chunk-vendors.1a04c04c.js></script><script src=static/js/app.a9157c72.js></script></body></html>
str;    
      }

    public function hell($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
    public function addDossier()
    {
        $data = Request::post();
        $dossier=Dossier::create($data);
        return $dossier->id;
    }
    public function addLend()
    {
        $data = Request::post();
        
        $lendLog=LendLog::create($data);
        Dossier::update(['id'=>$data['dossier_id'],'status'=>$data['in_out']]);
        
        return $lendLog->id;
    }
    public function editDossier()
    {
        $data = Request::post();
        $dossier=Dossier::update($data);
        return $dossier->id;
    }
    public function deleteDossier()
    {
        $dossier=Dossier::with(['thumbs','lend'])->find(Request::get());
        foreach($dossier->thumbs as $item){
          @unlink('storage/'.$item->url);
        }
        return $dossier->together(['thumbs','lend'])->delete();
    }
    public function fetchData()
    {
      $param=Request::get();
      // $map[]=["name", 'like', "%" . $param['name'] . "%"];
      foreach($param as $key=>$value){
        if(!in_array($key,['pageSize','pageIndex']))
        $map[]=[$key, 'like', "%" . $value . "%"];
      }
      
      $tableData=Dossier::where($map)->limit($param['pageSize']*($param['pageIndex']-1),$param['pageSize']*$param['pageIndex'])->order('id', 'desc')->select();
      $data=['tableData'=>$tableData,'pageTotal'=>Dossier::where($map)->count()];
      return json($data);
    }
    public function fetchThumb(){
      $data=Thumb::where(Request::get())->order('id','desc')->select();
      return json($data);
    }
    public function fetchLends(){
      $data=LendLog::where(Request::get())->order('id','desc')->select();
      return json($data);
    }
    public function upload(){
      $file = request()->file('image');
      $data=Request::post();
      $url = \think\facade\Filesystem::disk('public')->putFile( 'image', $file, 'data');
      $thumb=Thumb::create(['name'=>$file->getOriginalName(),'dossier_id'=>$data['id'],'url'=>$url]);
      return json($thumb);
    }
    public function delThumb(){
      return Thumb::destroy(Request::get('id'));
    }
  }   